﻿package KK.Hunter.MapEdit.Logic
{
	
	import KK.EMVC.View.View;
	import KK.GameBase.BaseUI;
	import KK.System.System;
	
	import flash.display.DisplayObjectContainer;
	import flash.display.Sprite;
	import flash.utils.ByteArray;
	import flash.utils.Dictionary;
	
	public class Lattice extends View implements ILattice
	{
		protected var _id:int;//id 
		protected var _x:Number;//widdth;
		protected var _y:Number;//height
		protected var _pixels:Number;//width and height ,this is rectangle
		protected var _diagonal:Number;//diagonal;
		
		protected var _attachedID:int;//attached id
		
		protected var _b_id:int;//建筑id
		protected var bytes:ByteArray;//lattice status
		protected var data:Dictionary;//lattices data
		
		
		protected var width:int;//宽多少格子
		protected var height:int;//高多少格子
		
		private var _pb_id:int;//玩家 建筑物 id
		public function Lattice()
		{
			super();
			init();
		}
		private function init():void
		{
			_attachedID=-1;
			data=new Dictionary();
			bytes=new ByteArray();
			this._id=-1;
			bytes[0]=bytes[1]=bytes[2]=bytes[3]=0;
			_pixels=_diagonal=_x=_y=0;
			this._b_id=0;
			writeData();
			
		}
		
		public function SetRect(
								id:int
								,b_id:int
								,x:Number
								,y:Number
								,pixels:Number
								,attachedID:int
							/*,ps:Sprite*/
		):void{
			this._id=id;
			this._b_id=b_id;
			_diagonal=pixels/Math.SQRT1_2;
			this.x=x;//+this._diagonal/2;
			this.y=y;
			this.attachedID=attachedID;
			this._pixels=pixels; 
			//graphics(ps);
		} 
		/*
		protected function graphics(ps:Sprite):void
		{
			ps.graphics.lineStyle(0.1,int(Math.random()));
			var i:int=0;
			
			var _th:Number=_diagonal/2;
			var _tw:Number=_diagonal;
			var th:int=this._diagonal/2; 
			
			var hp:Number=_th*0.01;
			var wp:Number=_tw*0.01;
			var x:int=this.x;
			var y:int=this.y;
			ps.graphics.lineTo(x,y);
			ps.graphics.moveTo(x+this._pixels*Math.SQRT1_2*2,y);
		}*/
		
		
		
		/*
		//set map data
		
		public function SetMapData(value:int):void
		{
			
//			this.bytes[2]=type;
//			this.bytes[1]=id;
//			this.bytes[0]=status;
			
			this.bytes.position=0;
			this.bytes.writeInt(value);
			
			
		}*/
		public function SetMapData(b_id:int,width:int,height:int):void{
			this._b_id=b_id;
			this.width=width;
			this.height=height;
		}
		public function SetBytes(byte:ByteArray):void
		{
			bytes.position=0;
			byte.position=0;
			this.bytes.writeBytes(byte,0,byte.length);
		}
		//get lattice data
		public function GetMapData():Dictionary{
			writeData();
			return this.data;
		}
		//test hit diamond
		public function TryDiamond(x:Number,y:Number):int{
			var result:int=0;
			bytes.position=2;
//			if(bytes.readByte()<1)
			{
				result=isRect(x-this._x,y-this._y,this._pixels);
			}
			return result;
			
		}
		protected function isRect(x:Number,y:Number,pixels:int):int
		{
			var result:int=0;//1为选中,0为没有在范围内
			var _th:Number=_diagonal/2;
			var _tw:Number=_diagonal;
			
			var mid:Number=_diagonal/2;
			
			var hp:Number=_th*0.01;
			var wp:Number=_tw*0.01;
			if(
				y>=0&&
				x>=0&&
				y<=_th&&
				x<=_tw
			)
			{
				if(y<_th/2)
				{
					if(x>=mid-(y/hp*wp)&&
						x<=mid+(y/hp*wp)){
						//上3角
						result=1;
					}
				}else{
					if(x>=mid-(_th/2-(y-_th/2))/hp*wp&&
						x<=mid+(_th/2-(y-_th/2))/hp*wp){
						//倒3角
						result=1;
					}
				}
			}
			return result;
		}
		//write lattice data
		protected function writeData():void{
			data.id=this.id; 
			data.b_id=this.b_id;
			data.x=x;
			data.y=y;
			data.pixels=_pixels;
			
			

			bytes.position=0;
			data.mapData=bytes;
		} 
		
		public function ShowByte():void
		{
			bytes.position=0; 
			bytes.position=0; 
		}

		public function set x(value:Number):void{
			this._x=value;
		}
		public function set y(value:Number):void{
			this._y=value
		}
		
		public function set attachedID(value:int):void{
			this._attachedID=value;
		}
		public function get x():Number{
			return this._x;
		}
		public function get y():Number{
			return this._y;
		}
		public function get id():int{
			return this._id; 
		}
		public function get attachedID():int{
			return this._attachedID;
		}
		public function get pixels():Number{
			return this._pixels;
		}
		public function get b_id():int{
			return this._b_id
		}
		public function get Width():int{
			return this.width;
		}
		public function get Height():int{
			return this.height;
		}
		public function get Level():int{
			bytes.position=0;
			return this.bytes.readInt();
		}
		public function set Level(value:int):void{
			bytes.position=0;
			bytes.writeInt(value);
		}
		public function get constructCount():int{
			return 0;
		}
		public function get pb_id():int{
			return this._pb_id;
		}
		public function set pb_id(value:int):void{
			this._pb_id=value;
			
		}
		
		
		public function get PosY():int{
			return this.x
		}
		public function get PosX():int{
			return this.y;
		}
	}
}